<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    
</body>
<script>
    // 
function ajax(url, options) {
    // your implement
  var type = options.type || 'GET';
  var dataType = options.dataType || 'json';
  var onsuccess = options.onsuccess || function(){};
  var onerror = options.onerror || function(){};
  var data = options.data || {};
  var dataStr = [];
  for(var key in data){
    dataStr.push(key + '=' + data[key]);
  }
  console.log(dataStr)
  dataStr = dataStr.join('&');
  console.log(dataStr)
  if(type === 'GET'){
    url += '?' + dataStr;
  }
  
  var xhr = new XMLHttpRequest();
  xhr.open(type, url, true);
  xhr.onload = function(){
    if((xhr.status >= 200 && xhr.status < 300) || xhr.status === 304){
      // 成功了
      if(dataType === 'json'){
        onsuccess(Json.parse(xhr.responseText));
      }else{
        onsuccess(xhr.responseText);
      }
    }else{
      onerror();
    }
  };
  xhr.onerror = onerror;
  if(type === 'POST'){
    xhr.send(dataStr);
  }else{
    xhr.send();
  }
}

// 使用示例：
ajax(
    'http://localhost:8080/server/ajaxtest', 
    {
        data: {
            name: 'simon',
            password: '123456'
        },
        onsuccess: function (responseText, xhr) {
            console.log(responseText);
        }
    }
);
</script>
</html>